/**
 * 
 */
package ch.il07.converter.business.impl;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.business.MeasurandAction;
import ch.il07.converter.business.UserBusiness;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.utils.RandomHelper;

/**
 * @author Fabio
 * 
 */
public class MeasurandActionImplTest {

	private MeasurandAction ma;
	private String username = RandomHelper.randString();
	
	private UserBusiness ub;

	@Before
	public void setUp() throws Exception {
		ma = new MeasurandActionImpl();
		ub = new UserBusinessImpl();
		ub.register(username, username,
				RandomHelper.getEmail(username),
				AccessLevel.USER.getAccessLevelId());
	}

	@Test
	public void testAddPrivateMeasurand() {
		String measurandName = RandomHelper.randString();
		assertTrue(ma.addPrivateMeasurand(username, measurandName, null,
				"Liter", null));
		assertFalse(ma.addPrivateMeasurand(username, measurandName, null,
				"Liter", null));
		try {
			ma.addPrivateMeasurand(null, "Entfernung", null, "Meter", null);
			fail("One of the attribute is null");
		} catch (IllegalArgumentException e) {
			// Expectet
		}
	}

	@Test
	public void testPublicMeasurandMethods() {
		String measurand = RandomHelper.randString();
		assertTrue(ma.addPublicMeasurand(measurand, null, "fabiometer", null));
		assertTrue(ma.deletePublicMeasurand(measurand));
	}

	@Test
	public void testDeletePrivateMeasurand() {
		String measurand = RandomHelper.randString();
		assertTrue(ma.addPrivateMeasurand(username, measurand, null, "Liter",
				null));
		assertTrue(ma.deletePrivateMeasurand(username, measurand));
	}

	@Test
	public void testGetAllPrivateMeasurands() {
		assertTrue(ma.addPrivateMeasurand(username, RandomHelper.randString(), null,
				"privateOne", null));
		assertTrue(ma.addPrivateMeasurand(username, RandomHelper.randString(), null,
				"privateTwo", null));

		List<Measurand> prMeasurands = ma.getAllPrivateMeasurands(username);
		assertTrue("expected size [2], actual size ["+prMeasurands.size()+"]", prMeasurands.size() == 2);
	}

	@Test
	public void testEditMeasurandName() {
		String measurandName = RandomHelper.randString();
		String baseUnitName = RandomHelper.randString();
		ma.addPublicMeasurand(measurandName, null, baseUnitName, null);
		Measurand measurand = null;
		for (Measurand m : ma.getAllPublicMeasurands()) {
			if (m.getName().equals(measurandName)) {
				measurand = m;
			}
		}
		assertNotNull(measurand);

		String newName = RandomHelper.randString();
		boolean edited = ma.editMeasurand(null, null, 
				measurand.getName(), newName, measurand.getDescription());
		assertTrue(edited);
		measurand = null;
		for (Measurand m : ma.getAllPublicMeasurands()) {
			if (m.getName().equals(newName)) {
				measurand = m;
			}
		}
		assertNotNull(measurand);
	}

	@Test
	public void testEditMeasurandDescription() {
		String measurandName = RandomHelper.randString();
		String baseUnitName = RandomHelper.randString();
		ma.addPublicMeasurand(measurandName, null, baseUnitName, null);
		Measurand measurand = null;
		for (Measurand m : ma.getAllPublicMeasurands()) {
			if (m.getName().equals(measurandName)) {
				measurand = m;
			}
		}
		assertNotNull(measurand);

		String newDescription = RandomHelper.randString();
		boolean edited = ma.editMeasurand(null, null, 
				measurand.getName(), measurand.getName(), newDescription);
		assertTrue(edited);
		measurand = null;
		for (Measurand m : ma.getAllPublicMeasurands()) {
			if (newDescription.equals(m.getDescription())) {
				measurand = m;
			}
		}
		assertNotNull(measurand);
	}

	@Test
	public void testEditMeasurandOwner() {
		String measurandName = RandomHelper.randString();
		String baseUnitName = RandomHelper.randString();
		String owner = RandomHelper.randString();
		assertTrue(ub.register(owner, owner, RandomHelper.getEmail(owner), AccessLevel.USER.getAccessLevelId()));
		assertTrue(ma.addPrivateMeasurand(owner, measurandName, null, baseUnitName, null));
		Measurand measurand = null;
		for (Measurand m : ma.getAllPrivateMeasurands(owner)) {
			if (m.getName().equals(measurandName)) {
				measurand = m;
			}
		}
		assertNotNull(measurand);

		String newOwner = RandomHelper.randString();
		assertTrue(ub.register(newOwner, newOwner, RandomHelper.getEmail(newOwner), AccessLevel.USER.getAccessLevelId()));
		assertTrue(ma.editMeasurand(owner, newOwner, measurand.getName(), 
				measurand.getName(), measurand.getDescription()));
		measurand = null;
		for (Measurand m : ma.getAllPrivateMeasurands(newOwner)) {
			if (newOwner.equals(m.getOwner().getUsername())) {
				measurand = m;
			}
		}
		assertNotNull(measurand);
	}
}
